/**
* This file is part of mobileworks.
*
* mobileworks is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* You should have received a copy of the GNU General Public License
* along with dev.clipall.  If not, see <http://www.gnu.org/licenses/>.
*
* https://code.google.com/p/mobileworks/
*/

/*
 * TitleBorder.java
 * Created on March 10, 2008, 12:40 PM
 */

package view.border;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import view.Color;
import view.Component;
import view.GuiUtils;
import view.font.Font;
import view.font.FontFactory;

/**
 *
 * @author Erol Hira
 */
public class TitleBorder extends Border{
    
    private Image image;
    private int contentMargin;
    private String title;
    
    private boolean imageTypeTitle;
    private Color bgColor;
    private Color fgColor;
    
    private int alignment = Graphics.LEFT | Graphics.VCENTER;
    
    private Font font = FontFactory.getDefaultFont();
    
    private byte rectangleTickness = 1;
    private byte focusedLostRectangleTickness = 1;
    private byte focusedRectangleTickness = 2;
    
    public TitleBorder(int w_left, int w_right, int w_top, int w_bottom, Image image, int title_dist_from_top, String title, Color fgColor) {
        
        super(w_left, w_right, w_top, w_bottom);
        
        this.image = image;
        
        imageTypeTitle = true;
        
        this.fgColor = fgColor;                
    }
    
    public TitleBorder(int w_left, int w_right, int w_top, int w_bottom, int contentMargin, String title, Color bgColor, Color fgColor) {
        
        super(w_left, w_right, w_top, w_bottom);
        
        this.bgColor = bgColor;
        this.fgColor = fgColor;
        this.title = title;
        this.contentMargin = contentMargin;
        imageTypeTitle = false;                                
    }
    
    public void paint(Graphics g, Component component) {
        
        if(imageTypeTitle){
            g.drawImage(image, component.getX(), component.getY(), Graphics.TOP | Graphics.LEFT);
        } else {
            GuiUtils.setColor(g, bgColor);
            g.fillRect(component.getX(), component.getY(), component.getWholeWidth() - 1, component.getWholeHeight() - 1);            
            GuiUtils.drawRectangle(g, component.getX(), component.getY(), component.getWholeWidth() - 1, component.getWholeHeight() - 1, fgColor, fgColor, rectangleTickness);
        }
        
        //g.drawString(title, component.get_x_content(), component.getY() + contentMargin, );        
        font.write(g, title, component.get_x_content(), 
                    component.getY() + contentMargin, 
                    component.getWidth(), w_top - contentMargin, 
                    alignment);
    }

    public void focus() {
        rectangleTickness = focusedRectangleTickness;
    }

    public void focusLost() {
        rectangleTickness = focusedLostRectangleTickness;
    }        
    
    public void setFont(Font font) {
        this.font = font;
    }

    public void setAlignment(int alignment) {
        this.alignment = alignment;
    }

    public byte getRectangleTickness() {
        return rectangleTickness;
    }

    public void setRectangleTickness(byte rectangleTickness) {
        this.rectangleTickness = rectangleTickness;
    }

    public byte getFocusedLostRectangleTickness() {
        return focusedLostRectangleTickness;
    }

    public void setFocusedLostRectangleTickness(byte focusedLostRectangleTickness) {
        this.focusedLostRectangleTickness = focusedLostRectangleTickness;
    }

    public byte getFocusedRectangleTickness() {
        return focusedRectangleTickness;
    }

    public void setFocusedRectangleTickness(byte focusedRectangleTickness) {
        this.focusedRectangleTickness = focusedRectangleTickness;
    }
}
